//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  FriendCell.m
//  Pizza
//
//  Created by ghwang on 15/3/28.
//  Copyright (c) 2015年 sendInfo. All rights reserved.
//

#import "FriendCell.h"

#pragma mark -

@implementation FriendPhoto

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
- (void)dataDidChanged{

    Friend *fr = self.data;
    BeeUIImageView *photo = (BeeUIImageView*)$(@"photo").view;
    
    

    NSString *url = [NSString stringWithFormat:@"%@/%@",[HttpRequest sharedInstance].imageUrl,fr.pic];
    [photo GET:url useCache:YES placeHolder:[UIImage imageFromString:@"img"]];
}

@end


@interface FriendCell(){

    BeeUITextField *_searchtf;

    UIView *addv;
}

@end

@implementation FriendCell

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_SIGNAL(SHAREBTN)
- (void)load
{
    

    
    self.list.lineCount = 4;
    self.list.animationDuration = 0.25f;
    self.list.whenReloading = ^{
    
        NSArray *friends = self.data;
        self.list.total = friends.count;
        for (BeeUIScrollItem *item in self.list.items) {
            
            
            item.size = CGSizeMake(self.list.bounds.size.width/4, 60);
            item.clazz = [FriendPhoto class];
            item.data = [friends safeObjectAtIndex:item.index];
        }
    
    
    };
    
    if ([SendIFAPPDefault shareAppDefault].userToken.length==0) {
        
        [self presentMessageTips:@"请先登录"];
        return;
    }
    
    self.MSG(HttpAPI.friend_list).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken});
    
}
ON_MESSAGE3(HttpAPI, friend_list, msg)
{
    if (msg.sending) {
        
        [self presentLoadingTips:@"正在获取"];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            self.data = msg.GET_OUTPUT(@"friends");
            [self.list asyncReloadData];
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }
    


}
- (void)unload
{
}

- (void)dataDidChanged
{
    // TODO: fill data
}

- (void)layoutDidFinish
{
    // TODO: custom layout here
}
ON_SIGNAL3(FriendCell, sharebtn, signal)
{
    NSArray *shareicons = @[[UIImage imageFromString:@"qq"],[UIImage imageFromString:@"weixin"],[UIImage imageFromString:@"tencent"],[UIImage imageFromString:@"sina"]];
    
    addv = [[UIView alloc] initWithFrame:CGRectMake(0, self.size.height+70, self.size.width, 70)];
    addv.backgroundImage = [UIImage imageFromString:@"friendbg"];
    [self addSubview:addv];
    
    for (int i = 0; i<4; i++) {
        
        
        BeeUIButton *shareBtn = [[BeeUIButton alloc] initWithFrame:CGRectMake(40+i*58, 10, 53, 53)];
        [shareBtn setBackgroundImage:shareicons[i] forState:UIControlStateNormal];
        shareBtn.tag = i+1010;
        [shareBtn addSignal:self.SHAREBTN forControlEvents:UIControlEventTouchUpInside object:shareBtn];
        [addv addSubview:shareBtn];
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        
        addv.frame = CGRectMake(0, self.size.height-70, self.size.width, 70);
        
    } completion:^(BOOL finished) {
        
        
    }];
    
    //
    self.MSG(HttpAPI.friend_invite).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken});
    
    
}
ON_SIGNAL3(FriendCell, addbtn, signal)
{
    
    
    addv = [[UIView alloc] initWithFrame:CGRectMake(0, self.size.height+70, self.size.width, 70)];
    addv.backgroundImage = [UIImage imageFromString:@"friendbg"];
    [self addSubview:addv];
    
    BeeUITextField *tf = [[BeeUITextField alloc] initWithFrame:CGRectMake(35, 10, self.size.width-70, 25)];
    _searchtf = tf;
    tf.backgroundColor = [UIColor blackColor];
    tf.textColor = [UIColor whiteColor];
    tf.layer.cornerRadius = 5;
    tf.layer.masksToBounds = YES;
    tf.layer.borderColor = [UIColor blackColor].CGColor;
    tf.layer.borderWidth = 1.0;
    tf.placeholder = @"请输入好友id";
    tf.borderStyle = UITextBorderStyleNone;
    [addv addSubview:tf];
    addv.backgroundColor = RGB(112, 112, 112);
    
    UIButton *starSearch = [[UIButton alloc] initWithFrame:CGRectMake(55, CGRectGetMaxY(tf.frame)+5, self.size.width-110, 25)];
    starSearch.backgroundColor = RGB(0, 0, 0);
    [starSearch addTarget:self action:@selector(addFriend) forControlEvents:UIControlEventTouchUpInside];
    starSearch.layer.cornerRadius = 5;
    starSearch.layer.masksToBounds = YES;
    [starSearch setTitle:@"添加" forState:UIControlStateNormal];
    [starSearch setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [addv addSubview:starSearch];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        addv.frame = CGRectMake(0, self.size.height-70, self.size.width, 70);
        
    } completion:^(BOOL finished) {
        
        
    }];
    
    
}
- (void)addFriend
{

    if (_searchtf.text.length == 0) {
        
        [self presentMessageTips:@"请输入好友id"];
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        
        addv.frame = CGRectMake(0, self.size.height+70, self.size.width, 70);
        
    } completion:^(BOOL finished) {
        
        
    }];
    self.MSG(HttpAPI.friend_add).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"memberId":_searchtf.text});
    

}
ON_MESSAGE3(HttpAPI, friend_add, msg)
{

    if (msg.sending) {
        
        [self presentLoadingTips:@"添加好友"];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            [self presentMessageTips:@"添加成功"];
           
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }


}
ON_MESSAGE3(HttpAPI, friend_invite, msg)
{

    if (msg.sending) {
        
        [self presentLoadingTips:@"获取邀请地址"];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            [self presentMessageTips:@"获取成功"];
             NSString *url = msg.GET_OUTPUT(@"inviteUrl");
            [[NSUserDefaults standardUserDefaults] setObject:url forKey:@"inviurl"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }


}

ON_SIGNAL3(FriendCell, chainbtn, signal)
{
    
}

@end
